package cn.edu.zucc.JavaChat.ChatClient.FileModule;

import java.util.ArrayList;

import cn.edu.zucc.JavaChat.ChatClient.FileModule.Event.FileTransferEvent;
import cn.edu.zucc.JavaChat.ChatClient.FileModule.Event.FileTransferListener;

public abstract class AbstractWithFileTransfer {
	private ArrayList<FileTransferListener> fileTransferListenerList;
	public synchronized void addFileTransferListener
			(FileTransferListener listener){
		if(fileTransferListenerList == null){
			fileTransferListenerList = new ArrayList<FileTransferListener>(2);
		}
		if(!fileTransferListenerList.contains(listener)){
			fileTransferListenerList.add(listener);
		}
	}
	
	public synchronized void removeFileTransferListener
		(FileTransferListener listener){
		if(fileTransferListenerList != null 
				&& fileTransferListenerList.contains(listener)){
			fileTransferListenerList.remove(listener);
		}
	}
	
	@SuppressWarnings("unchecked")
	protected void processEvent(FileTransferEvent e){
		ArrayList<FileTransferListener> list;
		synchronized(this){
			if(fileTransferListenerList == null) return;
			list = (ArrayList<FileTransferListener>)fileTransferListenerList.clone();
		}
		for(FileTransferListener item : list){
			item.FileTransferAction(e);
		}
	}
}
